package com.etc.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.etc.dao.UserMapper;
import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.utils.MD5Util;
import com.etc.utils.PageData;

@Service(value = "userService")
public class UserServiceImpl implements UserService {

	// 注入一个UserMapper对象
	@Resource
	private UserMapper userMapper;

	Logger logger = Logger.getLogger(UserServiceImpl.class);

	/**
	 * 分页+模糊查询
	 */
	@Override
	public PageData<User> getUserByPage(Integer page, Integer pageSize, String content, Integer ustate) {
		// TODO Auto-generated method stub

		// 起始位置
		int start = (page - 1) * pageSize;

		// 查询需要返回的数据
		List<User> data = userMapper.selectUserByPage(start, pageSize, content, ustate);
		int count = userMapper.selectCount(content);
		PageData<User> pd = new PageData<>(data, count, pageSize, pageSize);
		return pd;
	}

	/**
	 * 添加用户
	 */
	@Override
	public boolean addUser(User user) {
		// TODO Auto-generated method stub
		if (user == null) {
			return false;
		} else {
			user.setUpwd(MD5Util.getEncodeByMd5(user.getUpwd()));

			logger.info("userService:" + user);
			return userMapper.insert(user) > 0;
		}
	}

	/**
	 * 修改用户
	 */
	@Override
	public boolean updateUser(User user) {
		// TODO Auto-generated method stub
		if (user == null) {
			return false;
		} else {
			return userMapper.updateByPrimaryKeySelective(user) > 0;
		}
	}

	/**
	 * 删除用户
	 */
	@Override
	public boolean deleteUserById(int uid, int ustate) {
		// TODO Auto-generated method stub
		return userMapper.deleteUserById(uid, ustate) > 0;
	}

	// 登录
	@Override
	public User SelectUser(String utel, String upwd) {
		// TODO Auto-generated method stub

		return userMapper.SelectUser(utel, upwd);
	}

	// 注册
	@Override
	public int registUser(User user) {
		return userMapper.insert(user);

		// TODO Auto-generated method stub

	}

	@Override
	public List<User> getusers(Integer ustate) {
		// TODO Auto-generated method stub
		List<User> list = userMapper.getusersByustate(ustate);
		return list;
	}

}
